//
//  CommunityViewController.m
//  Zaker
//
//  Created by 赵磊 on 16/3/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommunityViewController.h"
#import "TopicCollectionCell.h"
#import "FindCollectionCell.h"

@interface CommunityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, ViewNaviBarDelegate, TopicPassDelegate>

@property (nonatomic, retain) TopScView *top;
@property (nonatomic, retain) ViewNaviBar *naviBar;
@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation CommunityViewController

//创建大collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 50);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50) collectionViewLayout:flow];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[TopicCollectionCell class] forCellWithReuseIdentifier:@"topic"];
    [self.collectionView registerClass:[FindCollectionCell class] forCellWithReuseIdentifier:@"find"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        TopicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topic" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor cyanColor];
        cell.delegate = self;
        return cell;
    } else {
        FindCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"find" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:0.502 green:1.000 blue:0.000 alpha:1.000];
        void (^block)() = ^(NSString *title, NSString *path) {
            FindDetailViewController *find = [[FindDetailViewController alloc] init];
            find.title = title;
            find.path = path;
            [self.navigationController pushViewController:find animated:YES];
        };
        cell.block = block;
        return cell;
    }
}

- (void)topicPassWithModel:(TopicModel *)model
{
    TopicDetailViewController *topic = [[TopicDetailViewController alloc] init];
    topic.model = model;
    [self.navigationController pushViewController:topic animated:YES];
}

- (void)PassNaviBarWithX:(CGFloat)x
{
    [_collectionView setContentOffset:CGPointMake(x * 5, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%f", scrollView.contentOffset.x);
    [self.naviBar naviBarChangeOffsetWithH:scrollView.contentOffset.x];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.navigationItem.title = @"社区";
    
    self.naviBar = [[ViewNaviBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    self.naviBar.backgroundColor = [UIColor colorWithRed:0.890 green:0.161 blue:0.188 alpha:1.000];
    self.naviBar.arr = @[@"话题", @"发现"].mutableCopy;
    self.naviBar.delegate = self;
    [self.view addSubview:_naviBar];
    
    [self createCollectionView];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    [_collectionView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
